//
//  HomeViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "HomeViewController.h"
#import "TitleModel.h"
#import "TitleCollectionViewCell.h"



@interface HomeViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)TVViewController *tv;
@property(nonatomic,strong)RecentActivityViewController *activity;
@property(nonatomic,strong)ChinaProjectTableViewController *chinaProject;
@property(nonatomic,strong)ForginProjectTableViewController *forginProject;
@property(nonatomic,strong)ChiaInfoTableViewController *chinaInfo;
@property(nonatomic,strong)ForginInfoTableViewController *forginInfo;
@property(nonatomic,strong)ColumnTableViewController *column;
@property(nonatomic,strong)LifeStyleTableViewController *lifeStyle;
@property(nonatomic,strong) UIScrollView *scrollView;

@end



@implementation HomeViewController
{
    NSArray *_nameArray;
    NSArray *_controlArray;
    NSMutableArray *_modelArray;
    NSIndexPath *_indexPath;
}

- (TVViewController *)tv
{
    if (!_tv) {
        self.tv = [[TVViewController alloc]init];
    }
    return _tv;
}

- (RecentActivityViewController *)activity
{
    if (!_activity) {
        self.activity = [[RecentActivityViewController alloc]init];
    }
    return _activity;
}

- (ChinaProjectTableViewController *)chinaProject
{
    if (!_chinaProject) {
        self.chinaProject = [[ChinaProjectTableViewController alloc]init];
    }
    return _chinaProject;
}

- (ForginProjectTableViewController *)forginProject
{
    if (!_forginProject) {
        self.forginProject = [[ForginProjectTableViewController alloc]init];
    }
    return _forginProject;
}

- (ChiaInfoTableViewController *)chinaInfo
{
    if (!_chinaInfo) {
        self.chinaInfo = [[ChiaInfoTableViewController alloc]init];
    }
    return _chinaInfo;
}

- (ForginInfoTableViewController *)forginInfo
{
    if (!_forginInfo) {
        self.forginInfo = [[ForginInfoTableViewController alloc]init];
    }
    return _forginInfo;
}

- (ColumnTableViewController *)column
{
    if (!_column) {
        self.column = [[ColumnTableViewController alloc]init];
    }
    return _column;
}


- (LifeStyleTableViewController *)lifeStyle
{
    if (!_lifeStyle) {
        self.lifeStyle = [[LifeStyleTableViewController alloc]init];
    }
    return _lifeStyle;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, kScreenWidth, kScreenHeight - 64 - 30 - 49)];
        self.scrollView.contentSize = CGSizeMake(kScreenWidth * 9, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:self.scrollView];
        self.scrollView.delegate = self;
    }
    return _scrollView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
   
    // 首页
    FirstViewController *first = [[FirstViewController alloc]init];
    first.view.frame = CGRectMake(0, 0, kScreenWidth, self.scrollView.height);
    [self addChildViewController:first];
    [self.scrollView addSubview:first.view];
    
    // TV
    self.tv.view.frame = CGRectMake(kScreenWidth * 1, 0, kScreenWidth, self.scrollView.bounds.size.height);
    [self addChildViewController:self.tv];
    [self.scrollView addSubview:self.tv.view];
    
    [self loadNavigationIndfo];
    [SDImageCache sharedImageCache].maxMemoryCost = 10;
    
    // 设置滑条
    [self setCollectionView];

    [SDImageCache sharedImageCache].maxMemoryCost = 1000;
}

#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    
    // 标题颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

#pragma mark scrollView代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   if (self.scrollView.contentOffset.x == kScreenWidth * 1)
    {
        // 近期活动
        self.activity.view.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, self.scrollView.bounds.size.height);
        [self addChildViewController:self.activity];
        [self.scrollView addSubview:self.activity.view];
    }
    else if (self.scrollView.contentOffset.x == kScreenWidth * 2)
    {
        // 国内创业
        self.chinaProject.view.frame = CGRectMake(kScreenWidth * 3, 0, kScreenWidth, self.scrollView.bounds.size.height);
        [self addChildViewController:self.chinaProject];
        [self.scrollView addSubview:self.chinaProject.view];
    }
    else if (self.scrollView.contentOffset.x == kScreenWidth * 3)
    {
        // 国外创业
        self.forginProject.view.frame = CGRectMake(kScreenWidth * 4, 0, kScreenWidth, self.scrollView.bounds.size.height);
        [self addChildViewController:self.forginProject];
        [self.scrollView addSubview:self.forginProject.view];
    }
    else if (self.scrollView.contentOffset.x == kScreenWidth * 4)
    {
        // 国内资讯
        self.chinaInfo.view.frame = CGRectMake(kScreenWidth * 5, 0, kScreenWidth, self.scrollView.height);
        [self addChildViewController:self.chinaInfo];
        [self.scrollView addSubview:self.chinaInfo.view];
    }
    else if (self.scrollView.contentOffset.x == kScreenWidth * 5)
    {
        // 国外资讯
        self.forginInfo.view.frame = CGRectMake(kScreenWidth * 6, 0, kScreenWidth, self.scrollView.height);
        [self addChildViewController:self.forginInfo];
        [self.scrollView addSubview:self.forginInfo.view];
    }
    else if (self.scrollView.contentOffset.x == kScreenWidth * 6)
    {
        // 专栏文章
        self.column.view.frame = CGRectMake(kScreenWidth * 7, 0, kScreenWidth, _scrollView.height);
        [self addChildViewController:self.column];
        [_scrollView addSubview:self.column.view];
    }
    else if (self.scrollView.contentOffset.x == kScreenWidth * 7)
    {
        self.lifeStyle.view.frame = CGRectMake(kScreenWidth * 8, 0, kScreenWidth, self.scrollView.height);
        [self addChildViewController:self.lifeStyle];
        [self.scrollView addSubview:self.lifeStyle.view];
    }

    for (TitleModel *model in _modelArray) {
        model.isClicked = NO;
    }
    // 将所有label设置成没有点击的颜色
    for (TitleCollectionViewCell *cell in _collectionView.visibleCells)
    {
        cell.nameLabel.textColor = [UIColor grayColor];
        cell.lineView.hidden = YES;
    }
    int page = _scrollView.contentOffset.x / kScreenWidth;
    
    // 将cell和model关联起来
    TitleModel *model = _modelArray[page];
    if (![scrollView isEqual:_scrollView]) {

    }
    else
    {
        [_collectionView scrollToItemAtIndexPath:model.myIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        if (page == 4)
        {
            _collectionView.contentOffset = CGPointMake(page * 70.0, 0);
        }
    }
    // 获取当前cell
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[_collectionView cellForItemAtIndexPath:model.myIndexPath];
    model.isClicked = YES;
    cell.nameLabel.textColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    cell.lineView.hidden = NO;
}

// 设置collectionView
- (void)setCollectionView
{
    _modelArray = [NSMutableArray array];
    _nameArray = @[@"首页推荐",@"视频资讯",@"移动互联",@"国内创业",@"国外创业",@"国内资讯",@"国外资讯",@"专栏文章",@"生活方式"];
    for (int i = 0; i < _nameArray.count; i++)
    {
        TitleModel *model = [[TitleModel alloc]init];
        model.title = _nameArray[i];
        model.isClicked = NO;
        [_modelArray addObject:model];
    }
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 左右滑
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, kScreenWidth, 30) collectionViewLayout:flowLayout];
    self.collectionView.contentSize= CGSizeMake(70 * _modelArray.count, 30);
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
}

#pragma mark collectionView代理方法返回item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _modelArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark collectionView返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    
    [collectionView registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:ID];
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    TitleModel *model = _modelArray[indexPath.row];
    cell.nameLabel.text = model.title;
    model.myIndexPath = indexPath;
    if (_scrollView.contentOffset.x == 0)
    {
        TitleModel *model = _modelArray[0];
        model.isClicked = YES;
    }

    if (model.isClicked == YES )
    {
        cell.nameLabel.textColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
        cell.lineView.hidden = NO;
    }
    else
    {
        cell.nameLabel.textColor = [UIColor grayColor];
        cell.lineView.hidden = YES;
    }
    return cell;
}

#pragma mark collectionView 点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    for (TitleModel *model in _modelArray) {
        model.isClicked = NO;
    }
    
    // 把所有的label设置成没有点击的颜色
    for (TitleCollectionViewCell *cell in collectionView.visibleCells)
    {
        cell.nameLabel.textColor = [UIColor grayColor];
        cell.lineView.hidden = YES;
    }
    
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    // 找到与之对应的model
    // 状态置成YES
    TitleModel *model = _modelArray[indexPath.row];
    model.isClicked = YES;
    cell.nameLabel.textColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    [UIView animateWithDuration:1 animations:^{
        cell.lineView.hidden = NO;
    }completion:nil];
    
    [UIView animateWithDuration:0.5 animations:^{
        _scrollView.contentOffset = CGPointMake(indexPath.row * kScreenWidth, 0);
    } completion:nil];
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark 返回cell尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   return CGSizeMake(70, 30);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache]clearMemory];
}



@end
